package com.shxy.rlzy.service.impl;

import com.shxy.rlzy.entity.SalaryTaskDepart;
import com.shxy.rlzy.mapper.SalaryItemMapper;
import com.shxy.rlzy.mapper.SalaryTaskDepartMapper;
import com.shxy.rlzy.mapper.SalaryTaskMapper;
import com.shxy.rlzy.service.ISalaryTaskDepartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanhao
 * @since 2022-03-19
 */
@Service
public class SalaryTaskDepartServiceImpl extends ServiceImpl<SalaryTaskDepartMapper, SalaryTaskDepart> implements ISalaryTaskDepartService {
    @Autowired
    private SalaryTaskDepartMapper salaryTaskDepartMapper;
    @Autowired
    private SalaryItemMapper salaryItemMapper;
    @Autowired
    private SalaryTaskMapper salaryTaskMapper;

    @Override
    public List<SalaryTaskDepart> getDepartsByTaskId(Long taskId) {
        return salaryTaskDepartMapper.listByTaskId(taskId);
    }

    @Override
    public void finishTaskById(Long id) {
        // 1、将部门任务下的个人任务状态设置为完成
        salaryItemMapper.changeStatusByDepartTaskId(id, 3);
        // 2、将部门任务状态设置为完成
        salaryTaskDepartMapper.updateStatuById(id, 3);
    }

    @Override
    public long getDepartIdByTaskId(long taskDepartId) {
        SalaryTaskDepart std = salaryTaskDepartMapper.selectById(taskDepartId);
        if(std != null) {
            return std.getDepartId();
        }
        return 0;
    }

    @Override
    public boolean resetById(Long departTaskId) {
        // 重置部门下员工任务
        salaryItemMapper.resetByDepartTaskId(departTaskId, 2, 0);
        // 重置部门任务
        return salaryTaskDepartMapper.changeStatusById(departTaskId, 2) > 0;
    }
}
